package sms;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created with IntelliJ IDEA.
 * User: leep
 * Date: 15-5-6
 * Time: 下午1:29
 * To change this template use File | Settings | File Templates.
 */
public class Sms {
	public static class DOMParser {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

		//Load and parse XML file into DOM
		public Document parse(InputStream is) {
			Document document = null;
			try {
				//DOM parser instance
				DocumentBuilder builder = builderFactory.newDocumentBuilder();
				//parse an XML file into a DOM tree
				document = builder.parse(is);
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return document;
		}

//		1，您于2015-05-19 11:40在店铺[孙记三房]使用会员账户消费￥100.00，当前账户余额为￥20.00。【门铺集】
//
//				2，您于2015-05-19 11:40在店铺[孙记三房]使用会员积分消费￥10，当前账户可用积分为20。【门铺集】
//
//				3，您的账户于2015-05-19 11:40在店铺[孙记三房]成功进行[充100送20]充值，账户增加￥120，当前账户余额为￥200。【门铺集】
//
//				4，您的账户积分于2015-05-19 11:40在店铺消费20.00元，此单累积为2000积分，当前账户可用积分为20100。【门铺集】
//
//				5，店铺[孙记三房]请求使用您的会员账户进行支付，支付金额：20.10元，支付码：123456。请将此支付码告知店员确认支付。支付码十分钟内有效。如果不是您在进行消费，请忽略本短信。【门铺集】
//
//				6，店铺[孙记三房]请求设置您的会员账户支付密码。设置校验码：123456 ，请在终端上输入校验码并修改密码。十分钟内有效。密码请单独设置，请勿交给店员处理。【门铺集】
//
//				7，123456，门铺集商家注册验证码，五分钟内有效。【门铺集】

		public static void main(String[] args) throws IOException, SAXException {
//			String mobile = "15029258413";
//			String mobile = "15680572081";
//			String mobile = "13348983406";
//			String mobile = "18981702130";
			//String mobile = "18080858963";
			String[] mobiles = {"15029258413", "15680572081", "13348983406", "18981702130"};
			for (int i = 0; i < mobiles.length; i++) {
				String mobile = mobiles[i];
				String PostData = "sname=dlmpjkj0&spwd=B4tZJ7Vk&scorpid=&sprdid=1012818&sdst=" + mobile + "&smsg=" +

						java.net.URLEncoder.encode("店铺[孙记三房]请求设置您的会员账户支付密码。设置校验码：123456 ，请在终端上输入校验码并修改密码。十分钟内有效。密码请单独设置，请勿交给店员处理。【门铺集】", "utf-8");
				String ret = SMS(PostData, "http://cf.51welink.com/submitdata/Service.asmx/g_Submit");

				System.out.println(ret + ":" + new Timestamp(System.currentTimeMillis()) + "");
				System.out.println("mobile:" + mobile);
			}

		}

		public static String SMS(String postData, String postUrl) {
			try {
				//发送POST请求
				URL url = new URL(postUrl);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("POST");
				conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				conn.setRequestProperty("Connection", "Keep-Alive");
				conn.setUseCaches(false);
				conn.setDoOutput(true);

				conn.setRequestProperty("Content-Length", "" + postData.length());
				OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
				out.write(postData);
				out.flush();
				out.close();

				//获取响应状态
				if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
					System.out.println("connect failed!");
					return "";
				}
				//获取响应内容体
				String line, result = "";
				BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
				while ((line = in.readLine()) != null) {
					result += line + "\n";
				}
				in.close();
				return result;
			} catch (IOException e) {
				e.printStackTrace(System.out);
			}
			return "";
		}
	}
}
